package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.cabinet.bo.UpgradeInfoListBO;
import com.yunji.cabinet.dao.IUpgradeInfoDao;
import com.yunji.cabinet.domain.ContainerDO;
import com.yunji.cabinet.domain.UpgradeInfoDO;
import com.yunji.cabinet.enums.UpgradeTaskStatus;
import com.yunji.cabinet.mapper.UpgradeInfoMapper;
import com.yunji.cabinet.params.UpgradeInfoParam;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.base.IModifyStatusBaseBO;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UpgradeInfoDaoImpl extends BaseDaoImpl<UpgradeInfoMapper, UpgradeInfoDO> implements IUpgradeInfoDao {
    @Override
    public boolean checkWaitInfo(String taskCode) {
        return lambdaQuery().eq(UpgradeInfoDO::getTaskCode, taskCode).eq(UpgradeInfoDO::getInfoStatus, UpgradeTaskStatus.TO_BE_START.getCode()).exists();
    }

    @Override
    public boolean startTask(Long infoId) {
        return update(new LambdaUpdateWrapper<UpgradeInfoDO>()
                .set(UpgradeInfoDO::getStartTime, TimeUtils.getNowDate())
                .set(UpgradeInfoDO::getInfoStatus, UpgradeTaskStatus.IN_PROGRESS.getCode())
                .eq(UpgradeInfoDO::getInfoId,infoId));
    }

    @Override
    public List<UpgradeInfoDO> search(UpgradeInfoListBO bo) {
        return list(new LambdaQueryWrapper<UpgradeInfoDO>()
                .eq(BeanUtils.isNotNull(bo.getTaskCode()),UpgradeInfoDO::getTaskCode,bo.getTaskCode())
                .in(BeanUtils.isNotNull(bo.getCodeList()),UpgradeInfoDO::getTaskCode,bo.getCodeList())
                .eq(BeanUtils.isNotNull(bo.getWhetherRetry()),UpgradeInfoDO::getWhetherRetry,bo.getWhetherRetry()));
    }

    @Override
    public List<UpgradeInfoDO> getUpgradeInfoList(UpgradeInfoListBO bo) {
        MPJLambdaWrapper<UpgradeInfoDO> wrapper = new MPJLambdaWrapper<>();
        return selectJoinList(UpgradeInfoDO.class,wrapper.selectAll(UpgradeInfoDO.class)
                .leftJoin(ContainerDO.class, ContainerDO::getContainerId,UpgradeInfoDO::getContainerId)
                .eq(ContainerDO::getOnlineStatus, LogicCode.YES)
                .eq(BeanUtils.isNotNull(bo.getTaskCode()),UpgradeInfoDO::getTaskCode,bo.getTaskCode())
                .in(BeanUtils.isNotNull(bo.getCodeList()),UpgradeInfoDO::getTaskCode,bo.getCodeList())
                .eq(BeanUtils.isNotNull(bo.getWhetherRetry()),UpgradeInfoDO::getWhetherRetry,bo.getWhetherRetry()));
    }

    @Override
    public Integer countNumsByStatus(String taskCode, Integer status) {
        return lambdaQuery().eq(UpgradeInfoDO::getTaskCode,taskCode).eq(UpgradeInfoDO::getInfoStatus,status).count().intValue();
    }

    @Override
    public List<UpgradeInfoDO> searchPageList(UpgradeInfoParam param) {
        return pageList(param,new LambdaQueryWrapper<UpgradeInfoDO>()
                .eq(BeanUtils.isNotNull(param.getTaskCode()),UpgradeInfoDO::getTaskCode,param.getTaskCode())
                .eq(BeanUtils.isNotNull(param.getInfoStatus()),UpgradeInfoDO::getInfoStatus,param.getInfoStatus())
                .like(BeanUtils.isNotNull(param.getVersion()),UpgradeInfoDO::getVersion,param.getVersion())
                .like(BeanUtils.isNotNull(param.getChipType()),UpgradeInfoDO::getChipType,param.getChipType())
                .like(BeanUtils.isNotNull(param.getSiteName()),UpgradeInfoDO::getSiteName,param.getSiteName())
                .like(BeanUtils.isNotNull(param.getAgentName()),UpgradeInfoDO::getAgentName,param.getAgentName())
                .like(BeanUtils.isNotNull(param.getContainerName()),UpgradeInfoDO::getContainerName,param.getContainerName()));
    }

    @Override
    public boolean modifyWhetherRetry(IModifyStatusBaseBO bo) {
        return modifyStatusById(bo,UpgradeInfoDO::getInfoId,UpgradeInfoDO::getInfoStatus);
    }

    @Override
    public boolean checkCancel(Long infoId) {
        return lambdaQuery().eq(UpgradeInfoDO::getInfoStatus,UpgradeTaskStatus.BE_CANCEL.getCode()).eq(UpgradeInfoDO::getInfoId,infoId).exists();
    }

    @Override
    public boolean closeTaskByTaskCode(String taskCode) {
        return update(new LambdaUpdateWrapper<UpgradeInfoDO>()
                .set(UpgradeInfoDO::getInfoStatus,UpgradeTaskStatus.BE_CANCEL.getCode())
                .eq(UpgradeInfoDO::getTaskCode,taskCode));
    }
}
